package com.yugong.controller;

import com.github.pagehelper.PageInfo;
import com.yugong.common.Result;
import com.yugong.entity.vo.request.CourseRequestVO;
import com.yugong.entity.vo.response.CourseListResponseVO;
import com.yugong.service.CourseService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程控制器类
 *
 * @author yugong
 * @date 2024/2/22
 */
@RestController
@RequestMapping("/api/course")
public class CourseController {
    @Resource
    private CourseService courseService;

    /**
     * 根据课程名查询课程信息
     *
     * @param pageNum    当前页
     * @param pageSize   当前页数据个数
     * @param courseName 课程名
     * @return 课程信息
     */
    @GetMapping("/find-course-page")
    public Result findCoursePage(@RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "5") Integer pageSize,
                                 @RequestParam String courseName) {
        PageInfo<CourseListResponseVO> courseListResponseVOPageInfo = courseService.findCourse(pageNum, pageSize, courseName);
        return Result.success(courseListResponseVOPageInfo);
    }


    /**
     * 新增课程
     *
     * @param courseRequestVO 课程请求实体类
     * @return 响应码
     */
    @PostMapping("/add-course")
    public Result addCourse(@RequestBody CourseRequestVO courseRequestVO) {
        int addFlag = courseService.addCourse(courseRequestVO);
        return Result.success(addFlag);
    }

    /**
     * 修改课程
     *
     * @param courseRequestVO 课程请求实体类
     * @return 响应码
     */
    @PutMapping("/update-course")
    public Result updateCourse(@RequestBody CourseRequestVO courseRequestVO) {
        int updateFlag = courseService.updateCourseByCourseNo(courseRequestVO);
        return Result.success(updateFlag);
    }

    /**
     * 删除课程
     *
     * @param courseNo 课程号
     * @return 响应码
     */
    @DeleteMapping("/delete-course/{courseNo}")
    public Result deleteCourse(@PathVariable String courseNo) {
        int deleteFlag = courseService.deleteCourseByCourseNo(courseNo);
        return Result.success(deleteFlag);
    }

    /**
     * 查询课程
     *
     * @return 课程
     */
    @GetMapping("/find-course")
    public Result findCourse() {
        List<CourseListResponseVO> courseListResponseVOList = courseService.findCourse();
        return Result.success(courseListResponseVOList);
    }
}
